package com.eric.zk;

import org.apache.zookeeper.*;

import java.util.concurrent.CountDownLatch;

/**
 * @author chenw-i
 * @version 1.0.0
 * @ClassName ZKApplication.java
 * @Description TODO
 * @createTime 2021年05月10日 09:49:00
 */
public class ZKApplication {
    private static final String address = "localhost:2181";
    private static final int session_timeout = 5000;
    private static final CountDownLatch cout = new CountDownLatch(1);

    public static void main(String[] args)throws Exception {
        ZooKeeper zk = new ZooKeeper(address, session_timeout, new Watcher() {
            // 事件通知
            @Override
            public void process(WatchedEvent event)
            {
                // 事件状态
                Event.KeeperState state = event.getState();
                // 连接状态
                if (Event.KeeperState.SyncConnected == state)
                {
                    Event.EventType type = event.getType();
                    // 事件类型
                    if (Event.EventType.None == type)
                    {
                        cout.countDown();
                        System.out.println("--------zk开始连接.");
                    }
                }
            }
        });

        cout.await();
        //创建节点
        String create = zk.create("/tangjiandong_tang", "dong".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("-------创建节点： " + create);
        zk.close();

    }
}
